package org.mtdownload.dialogs.preferences;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.mtdownload.utils.PreferencesManager;

public class FTPPreferences extends PreferencePanel {
	private static final long serialVersionUID = 3258408439209341751L;

	private JComboBox transferTypeCombo=new JComboBox();
	private JTextField anonymousUserPassword=new JTextField(PreferencesManager.getAnonymousFTPPassword());
	
	public FTPPreferences() {
		super("FTP");
		init();
	}
	
	private void init(){
		setLayout(new GridBagLayout());
		transferTypeCombo.addItem(PreferencesManager.FTP_TYPE_ASCII);
		transferTypeCombo.addItem(PreferencesManager.FTP_TYPE_BINARY);
		transferTypeCombo.setSelectedItem(PreferencesManager.getFTPTransferType());
		JLabel transferTypeLabel=new JLabel("Transfer type:");
		transferTypeLabel.setDisplayedMnemonic('T');
		transferTypeLabel.setLabelFor(transferTypeCombo);
		JLabel anonymousUserPasswordLabel=new JLabel("Password for anonymous user:");
		anonymousUserPasswordLabel.setDisplayedMnemonic('P');
		anonymousUserPasswordLabel.setLabelFor(anonymousUserPassword);
		anonymousUserPassword.getDocument().addDocumentListener(new DocumentListener(){
			public void insertUpdate(DocumentEvent e) {
				getPreferencesListener().preferencesChanged();
			}
			public void removeUpdate(DocumentEvent e) {
				getPreferencesListener().preferencesChanged();				
			}
			public void changedUpdate(DocumentEvent e) {
				getPreferencesListener().preferencesChanged();
			}
		});
		transferTypeCombo.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e) {
				getPreferencesListener().preferencesChanged();
			}
		});
		
		add(transferTypeLabel,new GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(10,10,0,0),0,0));
		add(transferTypeCombo,new GridBagConstraints(1,0,1,1,1,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,new Insets(10,0,0,10),0,0));
		add(anonymousUserPasswordLabel,new GridBagConstraints(0,1,1,1,0,0,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(5,10,0,0),0,0));
		add(anonymousUserPassword,new GridBagConstraints(1,1,1,1,1,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,new Insets(5,0,0,10),0,0));
		add(new JPanel(),new GridBagConstraints(0,2,2,1,1,1,GridBagConstraints.WEST,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
	}

	@Override
	public void applyChanges() {
		PreferencesManager.setFTPTransferType((String)transferTypeCombo.getSelectedItem());
		PreferencesManager.setAnonymousFTPPassword(anonymousUserPassword.getText());
	}

}
